package com.yuntsg.nnsfcp;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.ContentType;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import common.vo.FetchVectorVo;
import common.vo.FetchVectorVoStep;
import common.vo.PubVectorImageRes;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class HutoolServer {
    public static void main(String[] args) {


        HttpUtil.createServer(8888)
                .addAction("/", (req, response) -> {

                    String path = req.getParam("path").toString();
                    List<String> pms = new ArrayList<>();

//                    String path = "view/1/2024-06-04/FD2625247D1F2A73F64185C087238760.jpg";

                    String url = StrUtil.format("http://{}:{}/detailsminio/{}/", "10.1.1.19", "7083", "oss");

                    String res = HttpUtil.get(url + path.replace("oss/", ""));
                    JSONObject u = JSONObject.parseObject(res);
                    if (u.getInteger("code").equals(200)) {
                        String data = u.getString("data");
                        List<Float> floats = JSON.parseArray(data, Float.class);
                        FetchVectorVo fetchVectorVo = new FetchVectorVo().setTopK(6000).setSource(1).setPmut("0").setSearchVector(floats.stream().map(Float::doubleValue).collect(Collectors.toList()));
                        String post = HttpUtil.post("http://centralapi.yuntsg.com/centralapi/fetchVector", JSONObject.toJSONString(fetchVectorVo));
                        JSONObject r = JSONObject.parseObject(post);
                        if (r.get("code").equals(0)) {
                            FetchVectorVoStep fetchVectorVoStep = new FetchVectorVoStep().setData(JSONObject.parseArray(r.getString("data"), PubVectorImageRes.class)).setTotal(r.getInteger("total"));
                            pms = fetchVectorVoStep.getData().stream().map(PubVectorImageRes::getPm).distinct().collect(Collectors.toList());
                        }
                    }

                    response.write(JSONObject.toJSONString(pms), ContentType.JSON.toString());

                })
                .start();


//


    }
}
